package jgl.vajra.management.dialogFragment;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.CertificationAuditBean;
import jgl.vajra.management.bean.OnLinePayBean;
import jgl.vajra.management.databinding.DialogFragmentCertificationAuditBinding;
import jgl.vajra.management.listener.DelayDetailsListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.dialogfragment.BaseDialogFragment;
import jgl.vajra.management.widget.manager.RxJavaHttpManager;
import jgl.vajra.management.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.management.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

public class CertificationAuditDialogFragment extends BaseDialogFragment<DialogFragmentCertificationAuditBinding> implements BaseBindingItemPresenter<CertificationAuditBean> {

    private SingleTypeBindingAdapter adapter;
    private int tag;
    private String logo;
    private CertificationAuditListener listener;

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetEdit);


        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SingleTypeBindingAdapter(getContext(), null, R.layout.item_dialog_certification_audit);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);


        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        RetrofitJsonManager.getInstance().getApiService().getCertificationAuditData(map)
                .compose(RxJavaHttpManager.applyTransformer())
                .subscribe(new ProgressObserver<List<CertificationAuditBean>>(true, null) {
                    @Override
                    public void _onNext(List<CertificationAuditBean> data) {
                        adapter.refresh(data);
                    }
                });


    }

    @Override
    public void initData(Context context) {
    }

    @Override
    public void initEvent(Context context) {

    }

    public int getLayoutRes() {
        return R.layout.dialog_fragment_certification_audit;
    }


    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    public void setData(int tag, String logo) {
        this.tag = tag;
        this.logo = logo;
    }

    //确定
    public void onDetermine(boolean type) {//type false 不通过  true 通过

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("tag", tag);
        map.put("logo", logo);

        if (type) {
            RetrofitJsonManager.getInstance().getApiService().approvedData(map)
                    .compose(RxJavaHttpManager.applyTransformer())
                    .subscribe(new ProgressObserver<String>(true, null) {
                        @Override
                        public void _onNext(String data) {
                            if (listener != null)
                                listener.onDetermine();
                            dismiss();
                        }
                    });
        } else {
            List<CertificationAuditBean> listData = adapter.getListData();
            List<Integer> reasonList = new ArrayList<>();

            for (int i = 0; i < listData.size(); i++) {
                if (listData.get(i).isClick) {
                    reasonList.add(listData.get(i).id);
                }
            }

            String reason = mBinding.etReason.getText().toString().trim();
            if (reasonList.size() == 0 && TextUtils.isEmpty(reason)) {
                ToastUtils.showShort("请输入或者选择原因");
                return;
            }

            Map<String, Object> mapBody = new HashMap<>();
            mapBody.put("logo", logo);
            mapBody.put("reason", reason);
            mapBody.put("reasonList", reasonList);

            RetrofitJsonManager.getInstance().getApiService().noApprovedData(map, mapBody)
                    .compose(RxJavaHttpManager.applyTransformer())
                    .subscribe(new ProgressObserver<String>(true, null) {
                        @Override
                        public void _onNext(String data) {
                            if (listener != null)
                                listener.onDetermine();
                            dismiss();
                        }
                    });
        }
    }

    @Override
    public void onItemClick(int position, CertificationAuditBean itemData) {
        itemData.isClick = !itemData.isClick;
        adapter.refresh();
    }

    public void setCertificationAuditListener(CertificationAuditListener listener) {
        this.listener = listener;
    }

    public interface CertificationAuditListener {
        void onDetermine();
    }
}

